package com.abc.recycling.mapper;

import com.abc.recycling.domain.SysLog;
import org.apache.ibatis.annotations.*;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface SysLogMapper {
    
    // 插入日志
    @Insert("INSERT INTO sys_log(title, type, method, request_method, user_id, username, " +
            "operate_ip, operate_location, browser, os, request_url, request_param, " +
            "response_result, error_msg, status, operate_time, cost_time) " +
            "VALUES(#{title}, #{type}, #{method}, #{requestMethod}, #{userId}, #{username}, " +
            "#{operateIp}, #{operateLocation}, #{browser}, #{os}, #{requestUrl}, #{requestParam}, " +
            "#{responseResult}, #{errorMsg}, #{status}, #{operateTime}, #{costTime})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(SysLog log);
    
    // 根据ID查询日志
    @Select("SELECT * FROM sys_log WHERE id = #{id}")
    SysLog findById(Long id);
    
    // 搜索日志
    @Select("<script>" +
            "SELECT * FROM sys_log WHERE 1=1 " +
            "<if test='title != null and title != \"\"'> AND title LIKE CONCAT('%',#{title},'%')</if> " +
            "<if test='type != null and type != \"\"'> AND type = #{type}</if> " +
            "<if test='username != null and username != \"\"'> AND username LIKE CONCAT('%',#{username},'%')</if> " +
            "<if test='status != null'> AND status = #{status}</if> " +
            "<if test='startTime != null'> AND operate_time &gt;= #{startTime}</if> " +
            "<if test='endTime != null'> AND operate_time &lt;= #{endTime}</if> " +
            "ORDER BY operate_time DESC" +
            "</script>")
    List<SysLog> search(String title, String type, String username, Integer status, 
                       LocalDateTime startTime, LocalDateTime endTime);
    
    // 清空指定日期之前的日志
    @Delete("DELETE FROM sys_log WHERE operate_time < #{time}")
    int cleanBefore(LocalDateTime time);
    
    // 批量删除日志
    @Delete("<script>" +
            "DELETE FROM sys_log WHERE id IN " +
            "<foreach collection='ids' item='id' open='(' separator=',' close=')'>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    int deleteByIds(List<Long> ids);
    
    // 统计日志数量
    @Select("<script>" +
            "SELECT COUNT(*) FROM sys_log WHERE 1=1 " +
            "<if test='type != null and type != \"\"'> AND type = #{type}</if> " +
            "<if test='status != null'> AND status = #{status}</if> " +
            "<if test='startTime != null'> AND operate_time &gt;= #{startTime}</if> " +
            "<if test='endTime != null'> AND operate_time &lt;= #{endTime}</if> " +
            "</script>")
    int count(String type, Integer status, LocalDateTime startTime, LocalDateTime endTime);
} 